<?php


namespace app\api\controller;

use \app\common\business\UserAddress as UserAddressBiz;
use app\common\lib\Show;

class UserAddress extends AuthBase
{
    /**
     * 地址列表
     * @return \think\response\Json
     */
    public function lists() {
        $res = (new UserAddressBiz())->getList($this->userId);
        return Show::success($res);
    }

    /**
     * 添加
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function add() {
        if (!$this->request->isPost()) {
            return Show::error('非法访问');
        }
        $recevier_user = $this->request->param('recevier_user', '', 'trim');
        $recevier_phone = $this->request->param('recevier_phone', '', 'trim');
        $postal_code = $this->request->param('postal_code', '', 'trim');
        $province_id = $this->request->param('province_id', 0, 'intval');
        $city_id = $this->request->param('city_id', 0, 'intval');
        $district_id = $this->request->param('district_id', 0, 'intval');
        $address = $this->request->param('address', '', 'trim');
        //简单验证
        if ($recevier_user == '' || $recevier_phone == '' || $postal_code == '' || $province_id == 0 || $city_id == 0 || $district_id == 0 || $address == '') {
            return Show::error('参数错误');
        }
        $data = [
            'user_id' => $this->userId,
            'recevier_user'=> $recevier_user,
            'recevier_phone'=> $recevier_phone,
            'postal_code'=> $postal_code,
            'province_id'=> $province_id,
            'city_id'=> $city_id,
            'district_id'=> $district_id,
            'address'=> $address
        ];
        $res = (new UserAddressBiz())->add($data);
        if ($res) {
            return Show::success($res);
        }
        return Show::error();
    }

    /**
     * 删除
     * @param int $id
     * @return \think\response\Json
     */
    public function delete($id=0) {
        if (!$id) {
            return Show::error('参数错误');
        }
        $res = (new UserAddressBiz())->delete($this->userId, $id);
        if ($res) {
            return Show::success();
        }
        return Show::error();
    }

    /**
     * 修改
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function update() {
        if (!$this->request->isPost()) {
            return Show::error('非法访问');
        }
        $id = $this->request->param('id', 0, 'intval');
        $recevier_user = $this->request->param('recevier_user', '', 'trim');
        $recevier_phone = $this->request->param('recevier_phone', '', 'trim');
        $postal_code = $this->request->param('postal_code', '', 'trim');
        $province_id = $this->request->param('province_id', 0, 'intval');
        $city_id = $this->request->param('city_id', 0, 'intval');
        $district_id = $this->request->param('district_id', 0, 'intval');
        $address = $this->request->param('address', '', 'trim');
        //简单验证
        if ($id == 0 || $recevier_user == '' || $recevier_phone == '' || $postal_code == '' || $province_id == 0 || $city_id == 0 || $district_id == 0 || $address == '') {
            return Show::error('参数错误');
        }
        $data = [
            'id'=> $id,
            'recevier_user'=> $recevier_user,
            'recevier_phone'=> $recevier_phone,
            'postal_code'=> $postal_code,
            'province_id'=> $province_id,
            'city_id'=> $city_id,
            'district_id'=> $district_id,
            'address'=> $address
        ];
        $res = (new UserAddressBiz())->update($this->userId, $data);
        if ($res) {
            return Show::success($res);
        }
        return Show::error('操作失败');
    }

    /**
     * 默认收获地址
     * @param int $id
     * @return \think\response\Json
     */
    public function setDefault($id=0) {
        if (!$id) {
            return Show::error('参数错误');
        }
        $res = (new UserAddressBiz())->setDefault($this->userId, $id);
        if ($res) {
            return Show::success($res);
        }
        return Show::error('操作失败');
    }

}